import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;
import java.awt.event.*;
import java.util.Arrays; 
 
public final class Password extends JPanel                          
                          implements ActionListener {
    private static String OK = "ok";    

    private JFrame controllingFrame; //Skal bruges til dialogen    
    private JPasswordField passwordField;     

    public Password(JFrame f) {        
        //Bruger det vilkårlige layout.        
        controllingFrame = f;         
     

        //Lav det hele        
        passwordField = new JPasswordField(10);        
        passwordField.setActionCommand(OK);        
        passwordField.addActionListener(this);         

        JLabel label = new JLabel("Skriv password her: ");        
        label.setLabelFor(passwordField);         

        JComponent buttonPane = createButtonPanel();         
     
        JPanel textPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));        
        textPane.add(label);        
        textPane.add(passwordField);         

        add(textPane);        
        add(buttonPane);    
    }     

    protected JComponent createButtonPanel() {        
        JPanel p = new JPanel(new GridLayout(0,1));        
        JButton okButton = new JButton("OK");        

        okButton.setActionCommand(OK);        
        okButton.addActionListener(this);        

        p.add(okButton);        

        return p;    
    }     

    @Override
    public void actionPerformed(ActionEvent e) {        
        String cmd = e.getActionCommand();         

        if (OK.equals(cmd)) { //Bearbejder passwordet.            
            char[] input = passwordField.getPassword();            
	    if (isPasswordCorrect(input)) {                
		JOptionPane.showMessageDialog(controllingFrame,                   
		    "Korrekt! Du tastede det rigtige password.");           
            } else {                
                JOptionPane.showMessageDialog(controllingFrame,                    
		    "Ugyldigt password, prøv igen.",                    
		    "Fejl",                    
		    JOptionPane.ERROR_MESSAGE);            
	    }             
            
            Arrays.fill(input, '0');             

            passwordField.selectAll();            
            resetFocus();        
        }     
    }     

    /**     
     * Chekker det indtrykkede Array mod det rigtige password.      
     */    

    private static boolean isPasswordCorrect(char[] input) {        
        boolean isCorrect = true;        
	char[] correctPassword = { '1', '2', '3', '4', '5' };         

	if (input.length != correctPassword.length) {            
	    isCorrect = false;        
        } else {            
	    isCorrect = Arrays.equals (input, correctPassword);        
        }    
        
	Arrays.fill(correctPassword,'0');         

	return isCorrect;    
    }     
     
    protected void resetFocus() {        
        passwordField.requestFocusInWindow();    
    }     
  
    /**     
     * Laver Gui, og viser den.            
     */    
    private static void createAndShowGUI() {        
        //Laver vinduet.        
	JFrame frame = new JFrame("Password");        
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         

	//Laver indholdsboxen.        
	final Password newContentPane = new Password(frame);        
	newContentPane.setOpaque(true);        
	frame.setContentPane(newContentPane);         
	        
	frame.addWindowListener(new WindowAdapter() {            
            @Override
	    public void windowActivated(WindowEvent e) {                
		newContentPane.resetFocus();            
            }        
        });         

	//Viser vinduet.        
	frame.pack();        
	frame.setVisible(true);    
    }     
  
    public static void main(String[] args) {                
	SwingUtilities.invokeLater(new Runnable() {            
            @Override
	    public void run() {                
		        
	    UIManager.put("swing.boldMetal", Boolean.FALSE);        
	    createAndShowGUI();            
	    }        
	});    
    }
    
}
